/**
 * Created by Administrator on 2015/5/30.
 */
/*
* mode 权限位，0666 可读写！
* */

/*var fs = require('fs');
var BUFFER_SIZE = 1;
function copy(src,dest){
    var readSoFar=0,fdsrc,fddest,read;
    var buff = new Buffer(BUFFER_SIZE);
    fdsrc = fs.openSync(src,'r');
    fddest = fs.openSync(dest,'w');
    do{
        //fd, buffer, offset, length, position
        read  = fs.readSync(

            fd,buffer,0,BUFFER_SIZE,readSoFar)
        //fd, buffer, offset, length, position
        fs.writeSync(fd,buffer,0,read);
    }while(read>0)
    fs.closeSync(fdsrc);
    fs.closeSync(fddest);
}*/
/*
1 执行
2 写入
4 读取

fs.write
 fd 文件描述符
 buffer 从哪个缓冲区中读出内容
 offset 缓存区中读取的开始位置

 length 从缓冲区中读取的字节数
 positon 指定写入文件的开始位置
 callback
*/
var fs=require('fs');
fs.open('./ygl.txt','w',0666,function(err,fd){
    console.log(fd);
    fs.write(fd,new Buffer('虎妈猫爸'),3,6,0,function(err,bytesWrite,buffer){
        console.log('成功写入了'+bytesWrite+'字节');
        fs.fsync(fd,function(){  //fsync：先强行把缓冲区中的内容写入到文件中，然后在进行关闭。
            fs.close(fd,function(){
                fs.open('./ygl.txt','w',438,function(err,fd){
                    console.log('how old are you!'+fd);
                    fs.write(fd,new Buffer('真永远'),0,3,null,function(err,bytesWrite,buffer){
                        fs.write(fd,new Buffer('你好'),bytesWrite,3,3,function(err,bytesWrite,buffer){
                            console.log(bytesWrite);
                        });
                    });
                });
            });
        });

    });
});


/*多次写入文件*/
var fs=require('fs');


/*
* 当文件写入后，应该关闭这个文件
* 如果不关闭，他会一直占据这个索引
* */






























































